package servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Contact;
import domain.IDAOContact;
import domain.IDAOContactGroup;

public class PrepareContactModificationAction extends Action {
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest request,
			final HttpServletResponse response) {
		String id = request.getParameter("id");
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServlet().getServletContext());
		IDAOContact dao = (IDAOContact) context.getBean("daocontact");
		IDAOContactGroup daog = (IDAOContactGroup) context.getBean("daocontactgroup");
		request.setAttribute("groups", daog.getContactGroups().toArray());
		Contact contact = dao.getContact(Long.valueOf(id));
		if ( contact != null ) {
			request.setAttribute("contact", contact );
			return pMapping.findForward("modifyContact");
		}
		else {
			request.setAttribute("errorMessage", "The contact doesn't exist.");
			return pMapping.findForward("error");			
		}
	}
}
